package vn.nt.healthcare.server.ws.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import vn.nt.healthcare.server.ws.domain.Company;
import vn.nt.healthcare.server.ws.model.Reports;
import vn.nt.healthcare.server.ws.repositories.CompanyRepository;
import vn.nt.healthcare.server.ws.service.ReportsService;

@Service("reportService")
@Transactional
public class ReportsServiceImpl implements ReportsService {

	@Autowired
	private CompanyRepository companyRepository;

	@Override
	public Reports findReportById(String companyId, int id) {
		// TODO Auto-generated method stub
		Company company = companyRepository.findByCode(companyId);
		if (company != null) {
			Session session = null;
			try {
				session = ServiceHelper.openSession(company);
				Reports report = (Reports) session.get(Reports.class, id);
				return report;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				ServiceHelper.closeSession(session);
			}
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Reports> findReportsByStaff(String companyId, int staffId) {
		// TODO Auto-generated method stub
		Company company = companyRepository.findByCode(companyId);
		if (company != null) {
			Session session = null;
			try {
				session = ServiceHelper.openSession(company);
				StringBuilder sql = new StringBuilder(
						"SELECT DISTINCT r FROM Reports r WHERE 1 = 1");
				if (staffId > -1) {
					sql.append(" AND r.session.doctor.Id = :staffId");
				}
				sql.append(" ORDER BY r.session.sessionDate desc");
				Query query = session.createQuery(sql.toString());
				if (staffId > -1) {
					query.setInteger("staffId", staffId);
				}
				return query.list();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				ServiceHelper.closeSession(session);
			}
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Reports> findReportsByStaffInTime(String companyId,
			int staffId, Date fromDate, Date toDate) {
		Company company = companyRepository.findByCode(companyId);
		if (company != null) {
			Session session = null;
			try {
				session = ServiceHelper.openSession(company);
				StringBuilder sql = new StringBuilder(
						"SELECT DISTINCT r FROM Reports r WHERE 1= 1");
				if (staffId > -1) {
					sql.append(" AND r.session.doctor.Id = :staffId");
				}
				sql.append(" ORDER BY r.session.sessionDate >= :fromDate");
				sql.append(" ORDER BY r.session.sessionDate <= :toDate");
				sql.append(" ORDER BY r.session.sessionDate desc");
				Query query = session.createQuery(sql.toString());
				if (staffId > -1)
					query.setInteger("staffId", staffId);
				query.setTimestamp("fromDate", fromDate);
				query.setTimestamp("toDate", toDate);
				return query.list();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				ServiceHelper.closeSession(session);
			}
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Reports> findReportsByStaffIdAge(String companyId, int staffId,
			int fromAge, int toAge) {
		Company company = companyRepository.findByCode(companyId);
		if (company != null) {
			Session session = null;
			try {
				session = ServiceHelper.openSession(company);
				StringBuilder sql = new StringBuilder(
						"SELECT DISTINCT r FROM Reports r WHERE 1= 1");
				if (staffId > -1) {
					sql.append(" AND r.session.doctor.Id = :staffId");
				}
				sql.append(" ORDER BY r.bAge >= :fromAge");
				sql.append(" ORDER BY r.bAge <= :toAge");
				sql.append(" ORDER BY r.session.sessionDate desc");
				Query query = session.createQuery(sql.toString());
				if (staffId > -1)
					query.setInteger("staffId", staffId);
				query.setInteger("fromAge", fromAge);
				query.setInteger("toAge", toAge);
				return query.list();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				ServiceHelper.closeSession(session);
			}
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Reports findLastestReports(String companyId, int customerId) {
		// TODO Auto-generated method stub
		Company company = companyRepository.findByCode(companyId);
		if (company != null) {
			Session session = null;
			try {
				session = ServiceHelper.openSession(company);
				StringBuilder sql = new StringBuilder(
						"SELECT DISTINCT r FROM Reports r WHERE r.session.customer.Id = :customerId");
				sql.append(" ORDER BY r.session.sessionDate desc");
				Query query = session.createQuery(sql.toString());
				query.setInteger("customerId", customerId);
				query.setFirstResult(0);
				query.setMaxResults(1);
				List<Reports> list = query.list();
				if (!CollectionUtils.isEmpty(list)) {
					return list.get(0);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				ServiceHelper.closeSession(session);
			}
		}
		return null;
	}

}
